package com.tool.finances.lifestyle;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.tool.finances.lifestyle.data.AccountProvider;
import com.tool.finances.lifestyle.data.AccountType;

public class SearchActivity extends Activity {

	private SimpleDateFormat dateFormat;
	private TextView textStartDate;
	private TextView textEndDate;
	private CheckBox styleButton;
	private Spinner category;

	private String dateString;
	private String endDateString;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// setTheme(android.R.style.Theme_Dialog);
		setContentView(R.layout.search);
		super.onCreate(savedInstanceState);
		setTitle("Search setting");

		dateFormat = new SimpleDateFormat("yyyy/M/dd");
		Date date = new Date();
		dateString = dateFormat.format(date);
		date.setDate(date.getDate() + 1);

		endDateString = dateFormat.format(date);

		textStartDate = (TextView) findViewById(R.id.TextViewStart);
		textEndDate = (TextView) findViewById(R.id.TextViewEnd);
		textStartDate.setText(dateString);
		textEndDate.setText(endDateString);

		ImageButton imageButtonStart = (ImageButton) findViewById(R.id.ImageButton01Start);
		ImageButton imageButtonEnd = (ImageButton) findViewById(R.id.ImageButton02End);

		imageButtonStart.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					Date date = dateFormat.parse(textStartDate.getText()
							.toString());

					new DatePickerDialog(SearchActivity.this,
							new OnDateSetListener() {

								@Override
								public void onDateSet(DatePicker view,
										int year, int monthOfYear,
										int dayOfMonth) {
									String datetext = String.valueOf(year)
											+ "/"
											+ String.valueOf(monthOfYear + 1)
											+ "/" + String.valueOf(dayOfMonth);
									textStartDate.setText(datetext);
								}
							}, date.getYear() + 1900, date.getMonth(), date
									.getDate()).show();
				} catch (ParseException e) {

					e.printStackTrace();
				}
			}

		});
		imageButtonEnd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					Date date = dateFormat.parse(textEndDate.getText()
							.toString());

					new DatePickerDialog(SearchActivity.this,
							new OnDateSetListener() {

								@Override
								public void onDateSet(DatePicker view,
										int year, int monthOfYear,
										int dayOfMonth) {
									String datetext = String.valueOf(year)
											+ "/"
											+ String.valueOf(monthOfYear + 1)
											+ "/" + String.valueOf(dayOfMonth);
									textEndDate.setText(datetext);
								}
							}, date.getYear() + 1900, date.getMonth(), date
									.getDate()).show();
				} catch (ParseException e) {

					e.printStackTrace();
				}
			}

		});

		Button okButton = (Button) findViewById(R.id.Button01OK);
		Button cancelButton = (Button) findViewById(R.id.Button02Cancel);

		okButton.setOnClickListener(new OnClickListener() {

			@SuppressWarnings("null")
			@Override
			public void onClick(View v) {
				Date startDate = null;
				Date endDate = null;
				try {
					startDate = dateFormat.parse(textStartDate.getText()
							.toString());
					endDate = dateFormat
							.parse(textEndDate.getText().toString());
				} catch (ParseException e) {

					e.printStackTrace();
				}

				if (startDate.before(endDate)) {
					Intent data = new Intent();
					if (styleButton.isChecked()) {
						data.putExtra("selection",
								AccountType.Account.ACCOUNTTYPE + " = " + " '"
										+ category.getSelectedItem().toString()
										+ "' " + " and "
										+ AccountType.Account.ACCOUNTDATE
										+ " >= " + " '"
										+ textStartDate.getText().toString()
										+ "' " + " and "
										+ AccountType.Account.ACCOUNTDATE
										+ " <= " + " '"
										+ textEndDate.getText().toString()
										+ "' ");
					} else {
						data.putExtra("selection",
								AccountType.Account.ACCOUNTDATE + " >= " + " '"
										+ textStartDate.getText().toString()
										+ "' " + " and "
										+ AccountType.Account.ACCOUNTDATE
										+ " <= " + " '"
										+ textEndDate.getText().toString()
										+ "' ");
					}
					setResult(RESULT_OK, data);
					finish();
				} else {
					Toast.makeText(SearchActivity.this,
							"Please reset a correct time", 1000).show();
				}
			}
		});
		cancelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});

		Cursor cursor = getContentResolver().query(
				AccountProvider.CATEGORY_CONTENT_URI,
				AccountType.Account.CATEGORYPROECTION, null, null, null);
		String[] arrayList = getArrayList(cursor);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, arrayList);
		category = (Spinner) findViewById(R.id.Spinner01);
		category.setAdapter(adapter);
		styleButton = (CheckBox) findViewById(R.id.IsSpinnerEnable);
		styleButton.setChecked(false);
		styleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				category.setEnabled(isChecked);
			}

		});

		category.setEnabled(styleButton.isChecked());
	}

	private String[] getArrayList(Cursor cursor) {
		String[] arrayList = new String[cursor.getCount()];
		int i = 0;
		if (cursor.moveToFirst()) {
			do {
				int index = cursor
						.getColumnIndex(AccountType.Account.CATEGORYTYPE);
				arrayList[i] = cursor.getString(index);
				i++;
			} while (cursor.moveToNext());
		}

		return arrayList;
	}

}
